package cn.tom.controller.stu;

import cn.tom.dao.StuDao;
import cn.tom.dao.UserDao;
import cn.tom.entity.Mark;
import cn.tom.entity.Task;
import cn.tom.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/stu")
public class StuController {
    @Autowired
    UserDao userDao;
    @Autowired
    StuDao stuDao;

    @GetMapping("/find")
    public String find(Model model) {
        model.addAttribute("action", "update");
        return "stu/updatepwd";
    }

    @RequestMapping("/update")
    public String update(String phone, String pwd, String pwd1, String pwd2, Model model,HttpSession session) {
        System.out.println("手机号：" + phone);
        System.out.println("新密码=" + pwd2);
        User user = userDao.findOneByPhone(phone);//根据手机号查询
        System.out.println(user);
        Object who = session.getAttribute("who");
        System.out.println("who="+who);
        User use = (User) who;
        if (!phone.equals(use.getPhone())){//判断账号
            System.out.println("账号输入错误，请重新输入");
            model.addAttribute("pwq1","账号输入错误，请重新输入");
        }
        if (pwd.equals(pwd1)){      //判断新密码和旧密码是否相同
            model.addAttribute("action","update");
            System.out.println("新密码和原密码相同无法修改，请重新输入");
            model.addAttribute("pwd1","新密码和原密码相同无法修改，请重新输入");
            return "tea/logintea";
        }if (!pwd1.equals(pwd2)){   //判断两次新密码是否相同
            model.addAttribute("action","update");
            model.addAttribute("pwd1","两次新密码不同无法修改请重新输入");
            return "tea/logintea";
        }
        User user1= new User();
        user1.setPhone(phone);
        user1.setPwd(pwd2);
        user.setPwd(pwd1);
        System.out.println(user1);
        System.out.println(stuDao.update(user1));
        model.addAttribute("pwd1", "更新完毕！！！");
            return "stu/updatepwd";
    }
//  查询课程
    @GetMapping("/findcou")
    public String findCou(HttpSession session, Model model) {
        model.addAttribute("action", "findcou");//
        Object who = session.getAttribute("who");
        System.out.println("who="+who);
        User user = (User) who;
        String clzno= user.getClz().getClzno();
        System.out.println();
        List<Task> task =stuDao.findclzno(clzno);
        System.out.println("task="+task);
model.addAttribute("task",task);
        return "stu/updatepwd";
    }

//    查询分数
    @GetMapping("/findmark")
    public String findmark(HttpSession session,Model model){
        model.addAttribute("action","findmark");
        Object who = session.getAttribute("who");
        System.out.println("who="+who);
        User user = (User) who;
        List<Mark> marks=stuDao.findmark(user.getPhone());
        System.out.println(marks);
        model.addAttribute("mark",marks);
        return "stu/updatepwd";
    }
}

